package middle;

public class Solution_152 {

    public int maxProduct(int[] nums) {
        int res = nums[0], max = nums[0], min = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int num = nums[i];
            if (num > 0) {
                max = max > 1 ? max * num : num;
                min = min < 1 ? min * num : num;
            } else if (num < 0) {
                int tempMax = min < 1 ? min * num : num;
                int tempMin = max > 1 ? max * num : num;
                max = tempMax;
                min = tempMin;
            } else {
                max = 0;
                min = 0;
            }
            res = Math.max(max, res);
        }
        return res;
    }

    public static void main(String[] args) {
        Solution_152 model = new Solution_152();
//        System.out.println(model.maxProduct(new int[]{2, 3, -2, 4}));
//        System.out.println(model.maxProduct(new int[]{-2, 0, -1}));
        System.out.println(model.maxProduct(new int[]{-4, -3, -2}));
    }
}
